/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.quantum.jbencoder.types;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;

/**
 *
 * @author g073025
 */
public class BencodedString extends BencodedPrimitiveType {

    private String value = null;
    private final byte prefix = ':';

    public BencodedString() {
        super();
    }

    @Override
    protected void writePrefix(OutputStream os) throws IOException {
        //compute prefix as 'length of value' +  : ;
        os.write(BigInteger.valueOf(getValue().length()).toString().getBytes(charset));
        os.write(prefix);
    }

    @Override
    protected void writeValue(OutputStream os) throws IOException {
        os.write(getValue().getBytes(charset));
    }

    @Override
    protected void writeSuffix(OutputStream os) throws IOException {
        //do nothing
    }

    /**
     * @return the value
     */
    public String getValue() {
        return value;
    }

    /**
     * @param value the value to set
     */
    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public void deserialize(InputStream is) throws IOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
